/*
 * Copyright 2010 Mttang.com All right reserved. This software is the
 * confidential and proprietary information of Mttang.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Mttang.com.
 */
package com.mttang.sys.biz.service.support;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mttang.framework.core.service.AbstractService;
import com.mttang.info.core.ParameterModel;
import com.mttang.info.core.ParameterModelWrapper;
import com.mttang.info.core.ResultDTO;
import com.mttang.sys.biz.service.RoleService;
import com.mttang.sys.biz.service.SystemLogService;
import com.mttang.sys.dto.RoleDTO;
import com.mttang.sys.dto.RolePrivilegeDTO;
import com.mttang.sys.integration.dao.RoleDAO;
import com.mttang.sys.integration.dao.RolePrivilegeDAO;
import com.mttang.util.SysBeanConstant;

/**
 * 类RoleServiceImpl.java的实现描述：TODO 类实现描述 
 * @author acc 2010-6-1 下午07:55:43
 */
public class RoleServiceImpl  extends AbstractService implements   RoleService{

    private Log logger = LogFactory.getLog(getClass());
    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#delete(com.mttang.sys.dto.RoleDTO)
     */
    @Override
    public void delete(RoleDTO dto,Map<Object, Object> logMap) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleServiceImpl---delete");
        }
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        RolePrivilegeDAO  roleprv=(RolePrivilegeDAO)context.getBean(SysBeanConstant.DAO_ROLE_PRIVILEGE);
        RolePrivilegeDTO  prvdto=new RolePrivilegeDTO();
        prvdto.setRoleId(dto.getRoleId());
        roleprv.delete(prvdto);
        dao.delete(dto);
//      //写日志
//        SystemLogService systemLogService = (SystemLogServiceImpl)context.getBean(SysBeanConstant.SERVICE_SYSTEMLOG);
//        systemLogService.addLog(logMap);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#findById(java.lang.Integer)
     */
    @Override
    public RoleDTO findById(Integer id) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleServiceImpl---findById");
        }
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        return dao.findById(id);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#findCount(java.lang.String)
     */
    @Override
    public Long findCount(Map<String, Object> param) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleServiceImpl---findCount");
        }
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        return dao.findCount(param);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#findUser(java.lang.String)
     */
    @Override
    public ResultDTO<RoleDTO> findRole(ParameterModel parameter) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleServiceImpl---findUser");
        }
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        ParameterModelWrapper wrapper = new ParameterModelWrapper(parameter);
        Map<String, Object> param = wrapper.wrap();
        Long count = dao.findCount(param);
        parameter.getPagedFliper().setRecordCount(count.intValue());
        ResultDTO<RoleDTO> result = new ResultDTO<RoleDTO>();
        result.setDtoClass(RoleDTO.class);
        result.setTotalRecord(count);
        param = wrapper.wrap();
        result.setRecords(dao.findRole(param));
        return result;
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#insert(com.mttang.sys.dto.RoleDTO)
     */
    @Override
    public void insert(RoleDTO dto,Map<Object, Object> logMap) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleServiceImpl---insert");
        }
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        List<RolePrivilegeDTO> list=new ArrayList<RolePrivilegeDTO>();
        if(dto.getLinkId()!=null&&dto.getLinkId().length>0){
            for (int i = 0; i < dto.getLinkId().length; i++) {
                RolePrivilegeDTO roledto=new RolePrivilegeDTO();
                roledto.setLinkId(Integer.valueOf(dto.getLinkId()[i]));
                list.add(roledto);
            }
        }
        dao.insert(dto);
        RolePrivilegeDAO  prv=(RolePrivilegeDAO)context.getBean(SysBeanConstant.DAO_ROLE_PRIVILEGE);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                  if(list.get(i).getLinkId()!=-1){
                     prv.insertRole(list.get(i));
               }
            }
        }
//        
//      //写日志
//        SystemLogService systemLogService = (SystemLogServiceImpl)context.getBean(SysBeanConstant.SERVICE_SYSTEMLOG);
//        systemLogService.addLog(logMap);
        
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#update(com.mttang.sys.dto.RoleDTO)
     */
    @Override
    public void update(RoleDTO dto,Map<Object, Object> logMap) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleServiceImpl---update");
        }
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        List<RolePrivilegeDTO> list=new ArrayList<RolePrivilegeDTO>();
        if(dto.getLinkId()!=null&&dto.getLinkId().length>0){
            for (int i = 0; i < dto.getLinkId().length; i++) {
                RolePrivilegeDTO roledto=new RolePrivilegeDTO();
                roledto.setLinkId(Integer.valueOf(dto.getLinkId()[i]));
                roledto.setRoleId(dto.getRoleId());
                list.add(roledto);
            }
        }
        RolePrivilegeDAO  prv=(RolePrivilegeDAO)context.getBean(SysBeanConstant.DAO_ROLE_PRIVILEGE);
        RolePrivilegeDTO  prvdto=new RolePrivilegeDTO();
        prvdto.setRoleId(dto.getRoleId());
        prv.delete(prvdto);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                  if(list.get(i).getLinkId()!=-1){
                     prv.insert(list.get(i));
               }
            }
        }
         dao.update(dto);
         
//       //写日志
//         SystemLogService systemLogService = (SystemLogServiceImpl)context.getBean(SysBeanConstant.SERVICE_SYSTEMLOG);
//         systemLogService.addLog(logMap);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#findAll()
     */
    @Override
    public List<Map<String, String>> findAll() {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleServiceImpl---findAll");
        }
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        return dao.findAll();
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#findRoelPrv(java.lang.Integer)
     */
    @Override
    public List<RolePrivilegeDTO> findRoelPrv(Integer id) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleServiceImpl---findRoelPrv");
        }
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        return dao.findRoelPrv(id);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#findPrv(java.util.Map)
     */
    @Override
    public List<RolePrivilegeDTO> findPrv(Map<Object, Object> param) {
        // TODO Auto-generated method stub
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        return dao.findPrv(param);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.RoleService#findById(java.lang.String)
     */
    @Override
    public RoleDTO findByName(String roleName) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleServiceImpl---findById");
        }
        RoleDAO dao=(RoleDAO) context.getBean(SysBeanConstant.DAO_ROLE);
        return dao.findByName(roleName);
    }

}
